package screen;

import main.GameCanvas;
import main.NuoiThuMidlet;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import model.Command;
import model.IAction;
import service.GlobalService;
import utilities.GameResource;
import utilities.TField;
import font.BitmapFont;

public class SearchFriendScr extends Screen{
	static SearchFriendScr instance;
	public static SearchFriendScr gI(){
		if(instance==null)
			instance=new SearchFriendScr();
		return instance;
	}
	
	//name, level, money, gender
	private TField listTextField[];
	private String listText[] = {GameResource.name, GameResource.level, GameResource.gold, GameResource.gender};
	
	
	int wC, xC;
	int focus = 0;
	
	Command cmdClose;
	Command cmdFindFriend;
	Command cmdSelect;
	
	public Screen lastScr;

	//show screen
	public void switchToMe() {
		lastScr = GameCanvas.currentScreen;

		m_cmdRight = listTextField[0].cmdClear;
		m_cmdLeft = cmdClose;
		m_cmdCenter = cmdFindFriend;
		
		listTextField[3].setText(GameResource.male);
		
		super.switchToMe();
	}
	
	public SearchFriendScr(){	
		m_width = GameCanvas.w3d4;
		m_left = (GameCanvas.w - m_width)/2;
		
		m_height = 4 * (ITEM_HEIGHT + 2) + 2 * 5 + 30;
		m_top = (GameCanvas.h - m_height)/2;
		
		wC = m_width - 100;
		xC = m_left + 70;
				
		int y = m_top + 15 + (ITEM_HEIGHT + 2)/2;
		listTextField = new TField[4];
		for(int i = 0;i < 4; i++){
			listTextField[i] = new TField();
			listTextField[i].y = y;
			listTextField[i].width = wC;
			listTextField[i].height = ITEM_HEIGHT + 2;
			if(i == 1 || i == 2){
				listTextField[i].setIputType(TField.INPUT_TYPE_NUMERIC);
			}
			else{
				listTextField[i].setIputType(TField.INPUT_TYPE_ANY);
			}
			listTextField[i].isFocus = false;
			y += ITEM_HEIGHT + 4;
		}
		listTextField[0].isFocus = true;
		
		cmdFindFriend = new Command(GameResource.search, new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
				String tmp = listTextField[1].getText().trim();
				int iLev = 0;
				if(tmp.compareTo("") != 0)
					iLev = Integer.parseInt(tmp);
				tmp = listTextField[2].getText().trim();
				int Money = 0;
				if(tmp.compareTo("") != 0)
					Money = Integer.parseInt(tmp);
				tmp = listTextField[3].getText();
				int gender = (tmp.compareTo(GameResource.male) == 0)?1:0;
				
				tmp = listTextField[0].getText().toLowerCase();
				if(tmp.compareTo(NuoiThuMidlet.m_myPlayerInfo.m_strName.toLowerCase()) == 0){
					GameCanvas.startOKDlg("Tên tìm kiếm không hợp lệ");
					return;
				}
				
				GlobalService.gI().doSearchFriend(focus, listTextField[0].getText(), iLev, Money, gender);
				
				close();
			}
		});

		cmdClose = new Command(GameResource.close, new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
				close();
			}
		});
		
		cmdSelect = new Command(GameResource.select, new IAction() {			
			public void perform() {
				// TODO Auto-generated method stub
				Vector menu = new Vector();
				Command maleCmd = new Command(GameResource.male, new IAction() {
					public void perform() {
						// TODO Auto-generated method stub
						listTextField[3].setText(GameResource.male);
					}
				});
				menu.addElement(maleCmd);
				
				Command femaleCmd = new Command(GameResource.female, new IAction() {
					public void perform() {
						// TODO Auto-generated method stub
						listTextField[3].setText(GameResource.female);
					}
				});
				menu.addElement(femaleCmd);
				
				GameCanvas.menu.startAt(menu, 1);
			}
		});
	}

	public void close() {
		m_cmdCenter = null;
		m_cmdRight = null;
		m_cmdLeft = null;
		GameCanvas.cameraList.close();

		lastScr.switchToMe();
	}
	
	public void update() {
		lastScr.update();
		for(int i = 0;i<4;i++){
			TField txt = listTextField[i];
			
			if(txt.x != xC){
				txt.x += (xC - txt.x + 1) >> 1;
			}
			
			txt.update();
		}
	}
	
	public void updateKey() {
		boolean isCheckFocus = false;
		
		if (GameCanvas.isPointerClick) {
            int a = listTextField.length;
			for(int i = 0;i < a; i++){
				TField txt = listTextField[i];
				if(GameCanvas.isPointer(txt.x, txt.y, txt.width, txt.height)){
					focus = i;
					
					isCheckFocus = true;
				
					break;
				}
				
			}
		}
		
		
		if (GameCanvas.keyPressed[2]) {
			focus--;
			if (focus < 0)
				focus = listTextField.length - 1;
			
		} else if (GameCanvas.keyPressed[8]) {
			focus++;
			if (focus >= listTextField.length)
				focus = 0;
		} 
		
		if (GameCanvas.keyPressed[2] || GameCanvas.keyPressed[8] || isCheckFocus) {
			GameCanvas.clearKeyPressed();
			int a1 = listTextField.length;
			for(int i = 0;i < a1; i++){
				TField txt = listTextField[i];
				if(focus == i){
					txt.isFocus = true;	
					if(i == 3){
						m_cmdRight = cmdSelect;
					}
					else{
						m_cmdRight = txt.cmdClear;
					}
				}
				txt.isFocus = (focus == i);
			}
		}
		super.updateKey();
	}

	public void keyPress(int keyCode) {
                int a = listTextField.length - 1;
		for(int i = 0; i < a; i++){
			if(listTextField[i].isFocus)
				listTextField[i].keyPressed(keyCode);
		}

		super.keyPress(keyCode);
	}
	
	public void paint(Graphics g) {	
		lastScr.paint(g);
		
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(m_left, m_top, m_width, m_height);
		
		PaintPopup.paintRoundRect(g, m_left, m_top, m_width, m_height, 0x3c8dc0);
        		
		BitmapFont.drawBoldFont(g, GameResource.searchFriend, m_left + m_width/2, m_top + 5, 0x3a001e, Graphics.HCENTER);
		int a = listText.length;		
		for(int i = 0; i < a; i++){
			BitmapFont.drawNormalFont(g, listText[i], m_left + 10, listTextField[i].y + 4, 0x3a001e, Graphics.LEFT);
		}
		int a1 = listTextField.length;
		for(int i = 0; i < a1;i++){
			listTextField[i].paint(g);
		}

		super.paint(g);
	}
	    
    public void paintMarqueeMsg(Graphics g){
	}
}
	
